//
//  YHUMshareAlertView.m
//  LyhToolsTest
//
//  Created by LYH on 2018/10/27.
//  Copyright © 2018年 LYH.com. All rights reserved.
//

#import "YHUMshareAlertView.h"
#import "YHUMimageCell.h"
@interface YHUMshareAlertView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionview;
@property (nonatomic, strong) UIButton *disViewBtn;
@property (nonatomic, strong) NSMutableArray *images;
@end
@implementation YHUMshareAlertView

- (instancetype)initWithFrame:(CGRect)frame ImageArry:(NSArray *)images
{
    self=[super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.3];
        self.images = [NSMutableArray arrayWithArray:images];
        
        if (self.images.count ==0) {
            [self.images removeAllObjects];
            /**加载Bundle资源文件*/
            [self.images addObject:@"images.bundle/btn_share_moments"];
            [self.images addObject:@"images.bundle/btn_share_qq"];
            [self.images addObject:@"images.bundle/btn_share_wechat"];
            [self.images addObject:@"images.bundle/btn_share_weibo"];
            [self.images addObject:@"images.bundle/btn_share_zone" ];
        }
        [self collectionview];
        [self disViewBtn];
    }
    return self;
}
- (void)disAlterview
{
    //相对移动   参考点当前视图Fram（0，0）
    [UIView animateWithDuration:0.4 animations:^{
        self.collectionview.transform = CGAffineTransformMakeTranslation(0, 100);
    } completion:^(BOOL finished) {
         [self removeFromSuperview];
    }];
}
- (void)ShowAlterview
{
    UIWindow *keywindow = [UIApplication sharedApplication].keyWindow;
    [keywindow addSubview:self];
}
#pragma -mark ---------------------collectionView的代理和数据源方法---------------------------------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YHUMimageCell *cell = [YHUMimageCell codeCell:collectionView cellForItemAtIndexPath:indexPath];
    cell.ImageView.image = [UIImage imageNamed:self.images[indexPath.row]];
   
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"----%ld--行", indexPath.row);
    /**
     * self.delegate 判断一下控制器有没有遵守协议设置代理
     * [self.delegate respondsToSelector:@selector(didSelectItemAtIndex:)]判断有没有实现这个代理方法
     */
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectItemAtIndex:)]) {
        [self.delegate didSelectItemAtIndex:indexPath.row];
    }
}
#pragma -mark ————————————————————Lay————————————————————
//懒加载collectionview
- (UICollectionView *)collectionview
{
    if (!_collectionview) {
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;//水平滚动
        //根据分享个数创建布局
        NSInteger counts = self.images.count;
        TSLog(counts);
        layout.itemSize=CGSizeMake((kScreenWidth)/counts,(kScreenWidth)/counts);
        layout.minimumInteritemSpacing=0;
        layout.minimumLineSpacing=0;
        layout.sectionInset=UIEdgeInsetsMake(0, 0, 0, 0);
        
        _collectionview=[[UICollectionView alloc] initWithFrame:CGRectMake(0,kScreenHeigth-100-SafeAreaBoHttomHeight, kScreenWidth, 100) collectionViewLayout:layout];
        //相对移动   参考点当前视图Fram（0，0）
        _collectionview.transform = CGAffineTransformMakeTranslation(0, 100);
        [UIView animateWithDuration:0.4 animations:^{
            self.collectionview.transform = CGAffineTransformMakeTranslation(0, 0);
        }];
        _collectionview.dataSource=self;
        _collectionview.delegate=self;
        _collectionview.backgroundColor=[UIColor whiteColor];
        //支持分页
        _collectionview.pagingEnabled=NO;
        _collectionview.scrollEnabled=YES;
        [self addSubview:_collectionview];
    }
    return _collectionview;
}
- (UIButton *)disViewBtn
{
    if (!_disViewBtn) {
        _disViewBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeigth-100-SafeAreaBoHttomHeight)];
        [self addSubview:_disViewBtn];
        _disViewBtn.backgroundColor = [UIColor clearColor];
        WeakSelf;
        [[_disViewBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [wself disAlterview];
        }];
    }
    return _disViewBtn;
}

@end
